<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="借款额度">
        <el-input-number v-model="integralGrade.borrowAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间开始">
        <el-input-number v-model="integralGrade.integralStart" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间结束">
        <el-input-number v-model="integralGrade.integralEnd" :min="0" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate()"
        >
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import integralGradeApi from "@/api/core/integral-grade";
export default {
  data() {
    return {
      integralGrade: {},
      saveBtnDisabled: false,
    };
  },
  created() {
    if (this.$route.params.id) {
      this.getDataById(this.$route.params.id);
    }
  },
  methods: {
    getDataById(id) {
      integralGradeApi.getById(id).then((response) => {
        this.integralGrade = response.data.record;
      });
      this.saveBtnDisabled = false;
    },
    saveOrUpdate() {
      //对表格数据判断
      this.saveBtnDisabled = true; //禁用保存按钮
      if (
        !(
          this.integralGrade.borrowAmount &&
          this.integralGrade.integralStart &&
          this.integralGrade.integralEnd
        )
      ) {
        this.$confirm("没有填写数据,请填写完成!", "提示", {
          confirmButton: "确定",
          cancelButton: "取消",
          type: "warning",
        });
        this.saveBtnDisabled = false;
        return;
      } else if (this.$route.params.id) {
        //如果有id
        this.updateData();
      } else if (!this.$route.params.id) {
        //如果没有id
        this.saveData();
      }
    },
    updateData() {
      console.log("=======>即将修改数据");
      integralGradeApi.updateData(this.integralGrade).then((response) => {
        this.$message({
          type: "success",
          message: response.message,
        });
        this.$router.push("/core/integral-grade/list"); //实现页面跳转
      });
    },
    saveData() {
      console.log("=======>即将新增并保存数据");
      integralGradeApi.save(this.integralGrade).then((response) => {
        this.$message({
          type: "success",
          message: response.message,
        });
        this.$router.push("/core/integral-grade/list"); //实现页面跳转
      });
    },
  },
};
</script>
